using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MultiTCPProxy
{
    public partial class ProxyEditWindow : Window
    {
        private bool _isValid = false;

        public string ProxyName => txtName.Text.Trim();
        public string ListenPort => txtListenPort.Text.Trim();
        public string TargetIP => txtTargetIP.Text.Trim();
        public string TargetPort => txtTargetPort.Text.Trim();

        public ProxyEditWindow()
        {
            InitializeComponent();
            ValidateInputs();
        }

        private void ValidateInputs()
        {
            bool isValid = true;

            // 验证代理名称
            if (string.IsNullOrWhiteSpace(txtName.Text))
            {
                txtNameError.Visibility = Visibility.Visible;
                isValid = false;
            }
            else
            {
                txtNameError.Visibility = Visibility.Collapsed;
            }

            // 验证监听端口
            if (!IsValidPort(txtListenPort.Text))
            {
                txtListenPortError.Visibility = Visibility.Visible;
                isValid = false;
            }
            else
            {
                txtListenPortError.Visibility = Visibility.Collapsed;
            }

            // 验证目标IP
            if (!IsValidIP(txtTargetIP.Text))
            {
                txtTargetIPError.Visibility = Visibility.Visible;
                isValid = false;
            }
            else
            {
                txtTargetIPError.Visibility = Visibility.Collapsed;
            }

            // 验证目标端口
            if (!IsValidPort(txtTargetPort.Text))
            {
                txtTargetPortError.Visibility = Visibility.Visible;
                isValid = false;
            }
            else
            {
                txtTargetPortError.Visibility = Visibility.Collapsed;
            }

            _isValid = isValid;
            btnSave.IsEnabled = isValid;
        }

        private bool IsValidPort(string port)
        {
            if (string.IsNullOrWhiteSpace(port)) return false;
            if (!int.TryParse(port, out int portNumber)) return false;
            return portNumber >= 1 && portNumber <= 65535;
        }

        private bool IsValidIP(string ip)
        {
            if (string.IsNullOrWhiteSpace(ip)) return false;
            return IPAddress.TryParse(ip, out _);
        }

        private void TxtName_TextChanged(object sender, TextChangedEventArgs e)
        {
            ValidateInputs();
        }

        private void TxtListenPort_TextChanged(object sender, TextChangedEventArgs e)
        {
            ValidateInputs();
        }

        private void TxtTargetIP_TextChanged(object sender, TextChangedEventArgs e)
        {
            ValidateInputs();
        }

        private void TxtTargetPort_TextChanged(object sender, TextChangedEventArgs e)
        {
            ValidateInputs();
        }

        private void TxtPort_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !new Regex("[0-9]").IsMatch(e.Text);
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            if (!_isValid)
            {
                MessageBox.Show("请检查输入是否正确", "验证错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            DialogResult = true;
            Close();
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }
} 